﻿using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class System_Pages_Security_Users : SiteFox.Base.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            gv_Users.DataSource = SiteFox.Business.Manager.User.GetAllUsers();
            gv_Users.DataBind();
        }
    }

    #region ControlsEvents
    protected void imb_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_AddOrEdit.Visible = true;
        hf_Username.Value = "";
        lab_AddOrEdit_Title.Text = Resources.dcCommon.dc_Add;
        lib_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
        this.AdditionalEditFields(false);
    }
    protected void imb_Edit_Click(object sender, ImageClickEventArgs e)
    {
        pan_AddOrEdit.Visible = true;
        this.AdditionalEditFields(true);
        hf_Username.Value = ((ImageButton)(sender)).CommandArgument;
        lab_AddOrEdit_Title.Text = Resources.dcCommon.dc_Edit + " - " + hf_Username.Value;
        lib_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Edit;
        

        MembershipUser _User = Membership.GetUser(hf_Username.Value);
        ucc_Username.Value = _User.UserName;
        ucc_IsApproved.Value = _User.IsApproved;
        ucc_PasswordQuestion.Value = _User.PasswordQuestion;
        ucc_Email.Value = _User.Email;
        ucc_Comment.Value = _User.Comment;

        ucc_IsLockedOut.Value = _User.IsLockedOut;
        ucc_IsOnLine.Value = _User.IsOnline;
        ucc_CreationDate.Value = _User.CreationDate.ToShortDateString();
        ucc_LastActivityDate.Value = _User.LastActivityDate.ToShortDateString();
        ucc_LastLoginDate.Value = _User.LastLoginDate.ToShortDateString();
        ucc_LastLockoutDate.Value = _User.LastLockoutDate.ToShortDateString();
        ucc_LastPasswordChangedDate.Value = _User.LastPasswordChangedDate.ToShortDateString();
    }
    protected void imb_AddOrEdit_Close_Click(object sender, ImageClickEventArgs e)
    {
        pan_AddOrEdit.Visible = false;
        hf_Username.Value = "";
    }
    protected void lib_AddOrEdit_Ok_Click(object sender, EventArgs e)
    {
        if (hf_Username.Value == "")
        {
            MembershipCreateStatus _Status;
            string _PasswordQuestion = "";
            string _PasswordAnswer = "";

            if (Membership.RequiresQuestionAndAnswer)
            {
                _PasswordQuestion = ucc_PasswordQuestion.Value.ToString();
                _PasswordAnswer = ucc_PasswordAnswer.Value.ToString();
            }
            MembershipUser _NewUser = Membership.CreateUser(ucc_Username.Value.ToString(), tb_Password_Value.Text, ucc_Email.Value.ToString(), ucc_PasswordQuestion.Value.ToString(), ucc_PasswordAnswer.Value.ToString(), true, out _Status);
            for (int i = 0; i < UserRoles.Items.Count; i++)
            {
                ListItem _li = UserRoles.Items[i];
                if (_li.Selected)
                {
                    Roles.AddUserToRole(ucc_Username.Value.ToString(), _li.Text);
                }
            }
            if (_NewUser == null)
            {
                ucc_AddOrEdit_Message.Value = GetErrorMessage(_Status);
            }
            else
            {
                pan_AddOrEdit.Visible = false;
                hf_Username.Value = "";
                this.ResetAddOrEdit();
            }
        }
        else
        {
            MembershipUser _User = Membership.GetUser(hf_Username.Value);
            _User.Email = ucc_Email.Value.ToString();
            _User.Comment = ucc_Comment.Value.ToString();
            _User.IsApproved = Convert.ToBoolean(ucc_IsApproved.Value);
            Membership.UpdateUser(_User);
            for (int i = 0; i < UserRoles.Items.Count; i++)
            {
                ListItem _li = UserRoles.Items[i];
                if (_li.Selected)
                {
                    if (!Roles.IsUserInRole(hf_Username.Value, _li.Text))
                    {
                        Roles.AddUserToRole(hf_Username.Value, _li.Text);
                    }
                }
                else
                {
                    if (Roles.IsUserInRole(hf_Username.Value, _li.Text))
                    {
                        Roles.RemoveUserFromRole(hf_Username.Value, _li.Text);
                    }
                }
            }
            pan_AddOrEdit.Visible = false;
            hf_Username.Value = "";
            this.ResetAddOrEdit();
            this.AdditionalEditFields(false);
        }
    }
    protected void imb_Delete_Click(object sender, ImageClickEventArgs e)
    {
        //pan_Delete.Visible = true;
        //hf_Username.Value = ((ImageButton)(sender)).CommandArgument;
    }
    protected void imb_Delete_Close_Click(object sender, ImageClickEventArgs e)
    {
        //pan_Delete.Visible = true;
        //hf_Username.Value = "";
    }
    protected void lib_Delete_Ok_Click(object sender, EventArgs e)
    {
        //Membership.DeleteUser(hf_Username.Value, true);
        //pan_Delete.Visible = false;
    }
    #endregion

    #region Methods
    void ResetAddOrEdit() 
    {
        lab_AddOrEdit_Title.Text = "";
        ucc_Username.Value = "";
        ucc_IsApproved.Value = false;
        tb_Password_Value.Text = "";
        tb_Password2_Value.Text = "";
        ucc_PasswordQuestion.Value = "";
        ucc_PasswordAnswer.Value = "";
        ucc_Email.Value = "";
        ucc_Comment.Value = "";
        ucc_AddOrEdit_Message.Value = "";
        lib_AddOrEdit_Ok.Text = "";
    }
    void AdditionalEditFields(bool Visible) 
    {
        ucc_IsLockedOut.Visible = ucc_IsOnLine.Visible = ucc_CreationDate.Visible = ucc_LastActivityDate.Visible = ucc_LastLoginDate.Visible = ucc_LastLockoutDate.Visible = ucc_LastPasswordChangedDate.Visible = Visible;
        ucc_Username.Visible = pan_Password_Row.Visible = pan_Password2_Row.Visible = ucc_PasswordQuestion.Visible = ucc_PasswordAnswer.Visible = !Visible;
    }
    public string GetErrorMessage(MembershipCreateStatus status)
    {
        switch (status)
        {
            case MembershipCreateStatus.DuplicateUserName:
                return Resources.dcCommon.dc_DuplicateUserName;

            case MembershipCreateStatus.DuplicateEmail:
                return Resources.dcCommon.dc_DuplicateEmail;

            case MembershipCreateStatus.InvalidPassword:
                return Resources.dcCommon.dc_InvalidPassword;

            case MembershipCreateStatus.InvalidEmail:
                return Resources.dcCommon.dc_InvalidEmail;

            case MembershipCreateStatus.InvalidAnswer:
                return Resources.dcCommon.dc_InvalidAnswer;

            case MembershipCreateStatus.InvalidQuestion:
                return Resources.dcCommon.dc_InvalidQuestion;

            case MembershipCreateStatus.InvalidUserName:
                return Resources.dcCommon.dc_InvalidUserName;

            case MembershipCreateStatus.ProviderError:
                return Resources.dcCommon.dc_ProviderError;

            case MembershipCreateStatus.UserRejected:
                return Resources.dcCommon.dc_UserRejected;

            default:
                return Resources.dcCommon.dc_UnknownError;
        }
    }
    #endregion
}